###################################################################################################
## Payson (2020) "The Partisan Logic of City Mobilization" Replication Code
## RDD Analysis (Table 2 and Figures 4 - 6)
## Created with R version 3.4.1 (2017-06-30) -- "Single Candle"
###################################################################################################


## Set working directory
setwd()


## Load pacakges
library(rdd); library(rdrobust); library(rddtools); library(ggplot2)
library(AER); library(car); library(lmtest); library(plyr); library(rddensity)

rd <- read.csv("rdd.csv")


###################################################################################################
## Figure 4: RDD Sorting Test
###################################################################################################

par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(3, 0.6, 0))
DCdensity(rd$mov1, cutpoint = 0, plot = TRUE)
title(cex.lab = 1.2, xlab = "Margin of Victory for Unaligned Candidate", ylab = "Density") 
abline(v = 0, lty = 2)



###################################################################################################
## Table: RDD Results
###################################################################################################

## Simple RDD
summary(rdrobust(rd$lobby, rd$mov1, kernel = "triangular", p = 1, bwselect = "mserd", cluster = rd$fips))
summary(rdrobust(rd$lobby, rd$mov1, kernel = "triangular", p = 2, bwselect = "mserd", cluster = rd$fips))
summary(rdrobust(rd$lobby, rd$mov1, kernel = "triangular", p = 3, bwselect = "mserd", cluster = rd$fips))

## Covariate Adjusted
summary(rdrobust(rd$lobby, rd$mov1, kernel = "triangular", p = 1, bwselect = "mserd", cluster = rd$fips,
                 covs = cbind(rd$log.pop)))

summary(rdrobust(rd$lobby, rd$mov1, kernel = "triangular", p = 2, bwselect = "mserd", cluster = rd$fips,
                 covs = cbind(rd$log.pop)))

summary(rdrobust(rd$lobby, rd$mov1, kernel = "triangular", p = 3, bwselect = "mserd", cluster = rd$fips,
                 covs = cbind(rd$log.pop)))



###################################################################################################
## Figure 5: Effect of Narrowly Electing Aligned vs. Mismatched State Legislator on City Lobbying
###################################################################################################

par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(2, 0.6, 0))
rdplot(rd$lobby, rd$mov1, binselect = "es",  x.lim = c(-31, 31), y.lim = c(0, 1), nbins = c(15, 15), 
       ci = 95, x.label = "", y.label = "", col.lines = "black", title = "")
title(cex.lab = 1.2, ylab = "Probability of Lobbying", 
      xlab = "Margin of Victory for Unaligned Candidate")
points(x = rd$mov1, rd$lobby, pch = "|")



###################################################################################################
## Effect of Mismatch on Lobbying Across Bandwidths
###################################################################################################

## Delete missing values for use with rddtools
tmp <- na.omit(rdd_data(y = lobby, x = mov1, data = rd, cutpoint = 0, covar = data.frame(log.pop)))
summary(tmp)

## Be sure to load rddtools package. Use ?rdd_reg_lm and ?plotSensi for additional details
rd.lm <- rdd_reg_lm(rdd_object = tmp, order = 1, bw = 28, covariates = "log.pop")
summary(rd.lm)

sens <- plotSensi(rd.lm, from = 5, to = 30, vcov. = vcovCluster(rd.lm), 
                  order = 1, by = 1, plot = FALSE)
head(sens)

(p <- ggplot(sens, aes(x = bw, y = LATE)) +
    geom_line() + xlab("Bandwidth") + ylab("Effect on Probability of Lobbying") +
    geom_ribbon(aes(x = bw, ymin = CI_low, ymax = CI_high), alpha = .3) +
    geom_hline(yintercept = 0, linetype = 2) +
    theme_bw(base_size = 14, base_family = "serif") +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()))
